<?php
namespace App\Library\System\Model;

use App\Library\System\Model;
use Zend\View\Model\ViewModel;

class WidgetModel extends Model
{

    /**
     * Widget View
     *
     * @var ViewModel
     */
    public $view;

    /**
     * Widget layout folder
     *
     * @var type
     */
    public $layout_folder;


    public function __construct($id)
    {
        parent::__construct();

        if (is_numeric($id)) {
            $widget = $this->one($this->select()
                    ->from('widget')
                    ->where(array(
                        'id' => $id
                    ))
            );
        } else {
            $widget = $this->one($this->select()
                    ->from('widget')
                    ->where(array(
                        'alias' => $id
                    ))
            );
        }

        // Integrate widget's informations into this object as properties
        if ($widget) {
            $this->add($widget);
            $this->view = new ViewModel();
        } else {
            unset($this->id);
            $this->enable = false;
        }
    }

    // Widget is enable ?
    public function isEnable()
    {
        return ($this->enable == 1) ? true : false;
    }

    public function setTemplate($widget_name)
    {
        $this->view->setTemplate($this->layout_folder . '/' . $widget_name . '.phtml');
    }

    public function create()
    {
        return $this->view;
    }

}